package com.chucang.shucang.oms.controller;

import com.chucang.shucang.common.base.utils.R;
import com.chucang.shucang.common.security.annotation.Inner;
import com.chucang.shucang.oms.entity.CollectionOrderItemEntity;
import com.chucang.shucang.oms.service.CollectionOrderItemService;
import com.chucang.shucang.oms.vo.CollectionOrderItemsVO;
import com.github.yitter.idgen.YitIdHelper;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author flitsneak flitsneak@gmail.com
 * @since 1.0.0 2022-10-12
 */
@RestController
@RequestMapping("/collectionorderitem")
@RequiredArgsConstructor
@Api(tags = "藏品订单细项")
public class CollectionOrderItemController {

    private final CollectionOrderItemService collectionOrderItemService;

    @Inner
    @PostMapping("/addCollectionOrderItems")
    public R<String> addCollectionOrderItems(@RequestBody CollectionOrderItemsVO collectionOrderItemsVO) {
        List<CollectionOrderItemEntity> list = new ArrayList<>(30);
        for (int i = 0; i < collectionOrderItemsVO.getSerialNums().size(); i++) {
            CollectionOrderItemEntity collectionOrderItemEntity = new CollectionOrderItemEntity();
            collectionOrderItemEntity.setId(YitIdHelper.nextId());
            collectionOrderItemEntity.setOrderSn(collectionOrderItemsVO.getOrderSn());
            collectionOrderItemEntity.setUserId(collectionOrderItemsVO.getUserId());
            collectionOrderItemEntity.setSerialNum(collectionOrderItemsVO.getSerialNums().get(i));
            list.add(collectionOrderItemEntity);
        }
        collectionOrderItemService.saveBatch(list);
        return R.ok();
    }
}